<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="wangyuguo">
<title>角色定义</title>
<script type="text/javascript" language="JavaScript" src="../script/common.js"></script>
<script type="text/javascript" language="JavaScript" src="../script/tree.js"></script>
<script type="text/javascript" language="JavaScript">
var cNodeId = null;
//全局变量
var tree;
var qt = null;
var org = null;
var orgLevel = null;
var orgLevels;							//机构等级数组
var tempMap = {};

function load() {
    Eui.showNavigation();
    setPages(["appinfo","groupinfo","groupmods"]);
	org = Common.cOrg();
    orgLevels = SystemMgnt.orderOrgLevel();//对orgLevel排序
    for (var i = 0; i < orgLevels.length; i++) {
        tempMap[orgLevels[i].code] = orgLevels[i]
    }
    writeTree();
}
//模块授权页面 获取被选择群组方法
function getGroup() {
    return tree.getSelected().obj;
}

//模块授权页面 获取机构等级方法
function getOrgLevel() {
    return tree.getSelected().parentNode.obj.id;
}
/**树**/
function writeTree() {
    webTreeHandler.init();
    var p = Common.getItem("org_level", org.orgLevel);
    var obj = {id:p.code,name:p.text,isOrgLevel:true}
    tree = new LoadTree(obj.name, obj.id, "javascript:showProperty()", null, "../images/tree/root.png", "../images/tree/root.png");
    tree.obj = obj;
    $("acltree").innerHTML = tree;
    tree.select();
}
//树必须具备的方法，获取节点数据
function _startLoadXmlTree(sSrc, jsNode, newNode) {
    DWR.call("partyService.findGroupsByOrgLevel", sSrc, function(result) {
        if (sSrc < 700) {				//机构等级高于站级（700）时，为每个节点添加一个机构等级子节点
            var m = tempMap[sSrc].child
            var l = new Array([m.code,m.text,true]);
            result = l.concat(result);
        }
        _xmlFileLoaded(result, jsNode);
    });
}

//树必须具备的方法，生成树节点
function _createNode(data) {
    var obj = {id:data[0],name:data[1],isOrgLevel:data[2]};
    if (obj.id) {
        node = createTreeNode(!obj.isOrgLevel, obj.name, obj.id, "javascript:showProperty()");
    } else
        node = createTreeNode(true, "新角色", null, "javascript:showProperty()");
    node.obj = obj;
    return node;
}

//点击树节点触发的方法
function showProperty() {
    var node = tree.getSelected();
    var obj = node.obj;
    cNodeId = node.id;
    if (obj.id)
        formDisable(true)
    else
        formDisable(false)			//点击的是新建的节点，显示保存按钮
    if (node.isLeaf) {				//叶子节点，显示角色信息
        switchLink("group_a");
        showTab("groupinfo",$("tab_base"));
        switchPage("groupinfo");
       
        groupform.reset();
        if (obj.id)							//对于已经存在的角色，显示角色信息
            DWR.call("partyService.getGroup", obj.id, function(v) {
                Common.setValues(groupform, v);
            });
        else {
            var orgLevels = groupform.orgLevels.value;			//新建的角色，有几个层级的机构可以使用
            if (!orgLevels)
                orgLevels = "_";
            orgLevels += node.parentNode.src + "_"
            groupform.orgLevels.value = orgLevels;				//设置可使用该角色的层级机构
        }
    } else {
        switchLink("app_a");
        switchPage("appinfo");
    }
}
//显示页面顶部的tab
function switchLink(id) {
    var list = [];
    list.push($('app_a'));
    list.push($('group_a'));
    for (var i = 0; i < list.length; i++) {
        var o = list[i];
        if (o.id == id)
            o.style.display = "";
        else
            o.style.display = "none";
    }
}
//新建角色
function onNew() {
    var parentNode = tree;
    if (tree.getSelected()) {
        parentNode = tree.getSelected();
    }
    newNodeToTree(parentNode,{childAddText:"请选择一个机构等级，再增加角色"});
}
//保存角色信息
function saveGroup() {
	if (!checkForm(groupform)) return;
    var group = Common.getValues(groupform);
	if (group.description && group.description.length>24){
		showWarning("输入的描述字符超过24个！");return;
	}
    var treeNode = webTreeHandler.all[cNodeId];  				
    var ol = treeNode.parentNode.obj.id;
    var isNew = !group.id
    DWR.call("partyService.saveGroup", group, ol, function(result) {
        if (treeNode) {
            treeNode.obj = result;
            treeNode.setText(result.name);
            treeNode.isLeaf = true;
            treeNode.src = result.id;
        }
        if (!treeNode.isLeaf)
            treeNode.fileToFolder();
        showProperty();
        showInformation(isNew ? "增加角色【" + result.name + "】成功" : "修改角色【" + result.name + "】成功");
    },{blockScreen:true});
}

//对保存、编辑、取消按钮进行状态设置
function formDisable(status) {
    groupform.name.disabled = status
    groupform.uniqueId.disabled = status
    groupform.description.disabled = status
    groupform.savegroupbt.disabled = status
    groupform.canceleditbt.disabled = status
    groupform.editgroupbt.disabled = !status
}
//取消方法
function onCancel() {
	CardMgnt.resetForm(groupform)
    var node = tree.getSelected();
    if (!node.obj.id) {
        node.remove();
    }
    showProperty();
}
//删除角色方法
function onDelete() {
    var node = tree.getSelected();
    if (!node || node.obj.isOrgLevel) {
        showWarning("请选择需要删除的角色");
        return;
    }

    if (node.obj.type == "admin") {
        showError("无法删除系统角色");
        return;
    } else if (node.obj.id) {
        if (showQuestion("是否删除角色？")) {
            DWR.call("partyService.deleteGroup", node.obj.id, function() {
                node.remove();
                showProperty()
            });
        }
    } else {
        node.remove();
        showProperty()
    }
}
//选择角色基本属性
function showTab(id, obj) {
    var showModules;
    if (id =="groupmods") {
		var node = tree.getSelected();
		if (!node.obj.id){
			showError("未保存的角色，无法设置权限");
			return;
		}
    	showModules = function(){aclframe.document.location.href = "role_modules.html";}
    }
    switchPage(id,showModules);
    $("tab_base").className = "treeTab";
    $("tab_role").className = "treeTab";
    if (typeof obj == "string") obj = $(obj)
    obj.className = "treeTabc";
    return false;
}

</script>
</head>

<body onload="load()" class="noScroll">
<table cellSpacing=0 cellPadding=0 width="100%" border=0 height="100%">
    <tr>
        <td colspan="2">
            <div class="nav">
                <div id="nav">&nbsp;</div>
            </div>
        </td>
    </tr>
    <tr>
        <td width="29%" class="treeBtn treeBar">
            <a title="增加" href="javascript:void(0)" onclick="onNew()" onfocus="this.blur()" tabIndex="-1"><img src="../images/button/add.gif" border="0" align="absmiddle"> 增 加</a>
            <a title="删除" href="javascript:void(0)" onclick="onDelete()" tabIndex="-1"><img src="../images/button/del.gif" border="0" align="absmiddle"> 删 除</a>
        </td>
        <td width="68%" class="treeBar" valign=bottom>
            <div id="app_a">
            </div>
            <div id='group_a' style='display:none;'>
                <a href='#' onclick="showTab('groupinfo',this);" onfocus="this.blur()" tabindex="-1" id="tab_base" class="treeTabc">基本属性</a>
                <a href='#' onclick="showTab('groupmods',this);" onfocus="this.blur()" tabindex="-1" id="tab_role" class="treeTab">权限设定</a>
            </div>
        </td>
    </tr>
    <tr id="treeBody" class="treeBody">
        <td valign="top" width="29%">
            <div class="scrollDiv">
                <div id="acltree" class="treeDiv"></div>
            </div>
        </td>
        <td valign="top" width="68%">
            <div id="treeDet" class="scrollDiv">
                <div id='appinfo'></div>
                <div id='groupinfo' style='display:none;'>
                    <form id="groupform" name="groupform">
                        <input type="hidden" name="id">
                        <input type="hidden" name="orgLevels">

                        <div class="form">
                            <div class="formTitle">角色信息</div>
                            <table cellSpacing=0 cellPadding=4 width="100%" border=0>
                                <tr>
                                    <td align=center><label>角色编号</label></td>
                                    <td><input name="uniqueId" type="text"  maxlength="20" onblur="checkV(this,'',true)"><span class=des>*</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td align=center><label>名&nbsp;&nbsp;&nbsp;&nbsp;称</label></td>
                                    <td><input name="name" disabled type="text" maxlength="20" onblur="checkV(this,'',true)"><span
                                            class=des>*</span></td>
                                </tr>
                                <tr>
                                    <td align=center valign=top><label>描&nbsp;&nbsp;&nbsp;&nbsp;述</label></td>
                                    <td><textarea disabled name="description" rows=10 cols=30></textarea>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <p>
							<button type="button" name="editgroupbt" onclick="formDisable(false)">编 辑</button>
							<button type="button" name="savegroupbt" onclick="saveGroup()" disabled>保 存</button>
							<button type="button" name="canceleditbt" onclick="onCancel()" disabled>取 消</button>
                        </p>
                    </form>
                </div>
                <div id='groupmods' style='display:none;'>
                    <iframe id="aclframe" name="aclframe" style="width:97%;height:3000px;overflow:hidden" scrolling="no"
                            frameborder=0 src=""></iframe>
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>
</body>
</html>
